<?php
/*
	
*/
defined('BY_JYA') or exit('Access Denied');
global $_RQ,$_AR;
cfc('core')->checklogin();
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
if($_RQ['op']=='main'){
	set_html('title','升级会员');
	$return['member']=pdo_get('core_members',array('id'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),array('credit1','credit2','mobile','groupid','avatar','nickname'));
    $return['member']['avatar']=tomedia($return['member']['avatar']);
	$user=pdo_get('jy_merch_user',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
	$return['mygroup']=pdo_get('jy_merch_group',array('uniacid'=>$_SESSION['uniacid'],'id'=>$user['groupid']));
	$return['mygroup']['cssinfo']=json_decode($return['mygroup']['cssinfo'],true);
	$return['mygroup']['cssinfo']['fuwus']=str_array($return['mygroup']['cssinfo']['fuwus'],'|');
	$return['mygroup']['cssinfo']['keyword']=str_array($return['mygroup']['cssinfo']['keyword'],'|');
	$return['mygroup']['games']=str_array($return['mygroup']['games']);
	$games=array();
	foreach($return['mygroup']['games'] as $gv){
		$games[$gv]=$this->allitemgame[$gv];
	}
	$return['mygroup']['games']=$games;
	$return['mygroup']['level']=$return['mygroup']['level']?:0;
	if($user['viptime'] && $user['viptime']>TIMESTAMP){
		$user['viptime2']=date('Y-m-d',$user['viptime']);
	}elseif($user['viptime'] && $user['viptime']<TIMESTAMP){
		$user['viptime2']='已过期';
	}
	$parms=array('uniacid'=>$_SESSION['uniacid'],'status'=>1);
	if($parms){
		if($user['viptime']-TIMESTAMP>30*24*3600){
			$parms['level >']=$return['mygroup']['level'];
		}
	}
	$allgroup=pdo_getall('jy_merch_group',$parms,'','','level asc');
	$nowid=array();
	foreach($allgroup as &$av){
		$av['cssinfo']=$av['cssinfo']?json_decode($av['cssinfo'],true):array();
		if($av['cssinfo']['fuwus']){
			$av['fuwus']=str_array($av['cssinfo']['fuwus'],'|');
		}
		if($av['cssinfo']['keyword']){
			$av['keyword']=str_array($av['cssinfo']['keyword'],'|');
		}
		if($av['games']){
			$av['games']=str_array($av['games']);
			$games=array();
			foreach($av['games'] as $gv){
				$games[$gv]=$this->allitemgame[$gv];
			}
			$av['games']=$games;
		}
		$nowid[$av['id']]=pdo_getcolumn('jy_merch_viporder',array('level_id'=>$av['id'],'mid'=>$_SESSION['uid'],'ispay'=>0,'status'=>0),'id');
	}
	if($_RQ['xufee']){
		$nextlevel=$return['mygroup'];
	}else{
		$nextlevel=$allgroup[0];
	}
	$return['allgroup']=$allgroup;
	$return['nowgroup']=$allgroup[0];
	if(!$allgroup[0]){
		$return['nowgroup']=$return['mygroup'];
		$return['isnobuy']=1;
	}
	$post=array('level_id'=>$nextlevel['id']);
	$post['paytype']=1;
	$post['num']=1;
	$post['checked']=false;
	$return['xieyi']=plugin('jy_merch')->plugin_setting('xieyi');
	$return['post']=$post;
	$data=array();
	$data['isordermoney']=pdo_getsum('jy_weishop_order',array('mid'=>$_SESSION['uid'],'ispay'=>1),'price');
	$data['isordercount']=pdo_count('jy_weishop_order',array('mid'=>$_SESSION['uid'],'ispay'=>1));
	$return['data']=$data;
	$return['user']=$user;
	$nowid[$nextlevel['id']]=pdo_getcolumn('jy_merch_viporder',array('level_id'=>$nextlevel['id'],'mid'=>$_SESSION['uid'],'ispay'=>0,'status'=>0),'id');
	$return['nowid']=$nowid;
	$return['mens1']=array(
		array('name'=>'','title'=>'分享有礼','icon'=>'icon-all'),
		array('name'=>'','title'=>'自动成团','icon'=>'icon-all'),
		array('name'=>'','title'=>'客户管理','icon'=>'icon-all'),
		array('name'=>'','title'=>'管理员协作','icon'=>'icon-all'),
		array('name'=>'','title'=>'一键发货','icon'=>'icon-all'),
		array('name'=>'','title'=>'活动数据','icon'=>'icon-all'),
		array('name'=>'','title'=>'店铺数据','icon'=>'icon-all'),
		array('name'=>'','title'=>'订单导出','icon'=>'icon-all'),
		array('name'=>'','title'=>'分享有礼','icon'=>'icon-all'),
		array('name'=>'','title'=>'分享有礼','icon'=>'icon-all'),
	);
	$nowplugin=$_SESSION['current_plugin']?:'jy_quanhuo';
	$menus_index=$_SESSION['menus_index']?:$nowplugin.'/index/';
	$return['indexaction']=$menus_index;
	$return['homeaction']='jy_quanhuo/merch/';
	$return['tips']='当前暂未开通会员，开通付费会员可加倍助力营销！';
	exi($return);
}
if($_RQ['op']=='post'){
		$postdata=get_postdata();
		$groupid=intval($postdata['groupid']);
		$item=pdo_get('jy_merch_group',array('id'=>$groupid));
		if($item['price']<=0){
			exi('该等级不能付费开通！','error');
		}
		//创建一个会员订单
		$insert=array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$_SESSION['uid'],
			'level_id'=>$groupid,
			'ordersn'=>get_ordersn('jy_merch_viporder','ordersn'),
			'fee'=>$item['price'],
			'ispay'=>0,
			'status'=>0,
			'createtime'=>TIMESTAMP,
		);
		pdo_insert('jy_merch_viporder',$insert);
		$order_id=pdo_insertid();
		$params=array(
			'id'=>$order_id,
			'title'=>'开通商城会员',
			'fee'=>$item['price'],
			//'pay_type'=>'wechat',//可选，此项作用为指定支付方式
			'no_type'=>array('hdfk'),//可选，此项作用为指定不允许使用哪种支付方式
			'back_url'=>'jy_quanhuo/open/payresult/',//必填，指定异步支付结果通知地址，采用系统标准URL书写格式
			'success_url'=>'jy_quanhuo/merch/',
			'error_url'=>'jy_quanhuo/merch/',
		);
		$payid=cfc('pay')->create_pay_order($params);
		pdo_update('jy_merch_viporder',array('pay_id'=>$payid),array('id'=>$order_id));
		$res=array(
			'pay_id'=>$payid,
			'order_id'=>$order_id,
			'success_url'=>$success_url,
			'error_url'=>$error_url,
		);
		exi($res);
}
if($_RQ['op']=='pay'){
	$id = intval($_RQ['id']);
	if(!$id){
		exi('订单不存在，请重新选择订单','error');
	}	
	$order=pdo_get('jy_merch_viporder',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
	$item=pdo_get('jy_merch_group',array('id'=>$order['level_id']));
	if($item['price']<=0){
		exi('该等级不能付费开通！','error');
	}
	if($item['price']!=$order['fee']){
		pdo_update('jy_merch_viporder',array('fee'=>$item['price']),array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
		pdo_update('core_paylog',array('fee'=>$item['price']),array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['pay_id']));
	}
	exi($order['pay_id']);
}
if($_RQ['op']=='payresult'){
	$payresult=$_RQ['pay_result'];
	if(!$payresult){
		exi('支付失败，未收到支付结果！','error');
	}
	if($payresult['status']!=1){
		exi('支付失败！','error');
	}
	$order=pdo_get('jy_merch_viporder',array('id'=>$payresult['id'],'uniacid'=>$_SESSION['uniacid']));
	if(!$order){
		exi('支付失败！购买会员订单不存在！','error');
	}
	if($order['fee']!=$payresult['fee']){
		exi('支付失败！购买会员订单金额不匹配！','error');
	}
	pdo_update('jy_merch_viporder',array('status'=>1,'ispay'=>1,'paytime'=>TIMESTAMP,'paytype'=>$payresult['pay_type']),array('id'=>$order['id']));
	//计算到期时间
	$startTime =time();
	$member=pdo_get('jy_merch_user',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$order['mid']));
	if($member['groupid']==$order['level_id'] && $member['viptime']>TIMESTAMP){
		$addTime=$member['viptime']+365*24*3600;
	}else{
		$addTime =TIMESTAMP+365*24*3600;
	}
	pdo_update('jy_merch_user',array('viptime'=>$addTime,'groupid'=>$order['level_id']),array('mid'=>$order['mid'],'uniacid'=>$_SESSION['uniacid']));
	exi('支付成功','success','jy_quanhuo/merch/');
}